package com.ziven.learn.dispatch;

import android.graphics.Outline;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewOutlineProvider;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ziven.easygo.EasyGos;
import com.ziven.easygo.autowired.EasyGo;
import com.ziven.easygo.ui.AbstractBaseActivity;
import com.ziven.easygo.ui.AbstractEasyViewHolder;
import com.ziven.easygo.ui.AbstractTypeAdapter;
import com.ziven.easygo.util.EasyUtils;
import com.ziven.easygo.util.LogHelper;
import com.ziven.easygo.util.Nulls;
import com.ziven.easygo.util.ViewHelper;
import com.ziven.learn.LearnInterface;
import com.ziven.learn.R;
import com.ziven.learn.bean.MixedRowBean;
import com.ziven.learn.model.MixedRowModel;
import com.ziven.processor.Learn;

/**
 * @author Ziven
 */
@Learn(learn = "混排")
public class MixedRowLearn implements LearnInterface {

    @Override
    public void learn() {
        EasyGo.easyGo(MixedRowActivity.class);
    }

    public static class MixedRowActivity extends AbstractBaseActivity {

        private static final int SPAN_COUNT = 2;
        private final LeftMixedRowAdapter leftAdapter = new LeftMixedRowAdapter();
        private final RightMixedRowAdapter rightAdapter = new RightMixedRowAdapter();

        @Override
        protected Object obtainLayout() {
            return R.layout.activity_mixed_row;
        }

        @Override
        protected void initLayout() {
            ViewHelper.create(getView(R.id.left))
                    .setAdapter(leftAdapter)
                    .setLayoutManager(new LinearLayoutManager(this));

            EasyGos.getDispatch().setView(data -> {
                LogHelper.of("MixedRowModel-left").join(data).print();
                leftAdapter.getDataProvider().setList(data.getOneData());
                EasyUtils.notifyDataSetChanged(leftAdapter);
            }).getPresenter().setParam(MixedRowModel.GRID_VIEW, false).obtainOneData();

            GridLayoutManager glm = new GridLayoutManager(this, SPAN_COUNT);
            glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    MixedRowBean mrb = rightAdapter.getDataProvider().getData(position);
                    return mrb != null && mrb.getViewType() == MixedRowBean.TYPE_MULTI ? SPAN_COUNT : 1;
                }
            });

            ViewHelper.create((RecyclerView) getView(R.id.right))
                    .setLayoutManager(glm)
                    .setAdapter(rightAdapter);

            EasyGos.getDispatch().setView(data -> {
                LogHelper.of("MixedRowModel-right").join(data).print();
                rightAdapter.getDataProvider().setList(data.getOneData());
                EasyUtils.notifyDataSetChanged(rightAdapter);
            }).getPresenter().setParam(MixedRowModel.GRID_VIEW, true).obtainOneData();
        }

        @Override
        protected void destroyLayout() {

        }
    }

    public static class LeftMixedRowAdapter extends AbstractTypeAdapter<MixedRowBean, LeftMixedRowHolder> {

        @NonNull
        @Override
        public LeftMixedRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return viewType == MixedRowBean.TYPE_SINGLE
                    ? new LeftMixedRowHolder(parent, R.layout.item_mixed_row_single)
                    : new LeftMixedRowHolder(parent, R.layout.item_mixed_row_multi);
        }
    }

    public static class LeftMixedRowHolder extends AbstractEasyViewHolder<MixedRowBean> {

        public LeftMixedRowHolder(@NonNull ViewGroup parent, int layoutId) {
            super(parent, layoutId);
            getView(R.id.icon).setClipToOutline(true);
            getView(R.id.icon).setOutlineProvider(new ViewOutlineProvider() {
                @Override
                public void getOutline(View view, Outline outline) {
                    outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(),
                            view.getResources().getDimension(R.dimen.dp_6));
                }
            });

            Nulls.of((View) getView(R.id.icon2)).doNotNull(v -> {
                v.setClipToOutline(true);
                v.setOutlineProvider(new ViewOutlineProvider() {
                    @Override
                    public void getOutline(View view, Outline outline) {
                        outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(),
                                view.getResources().getDimension(R.dimen.dp_6));
                    }
                });
            });
        }

        @Override
        protected void bindLayout(MixedRowBean data, int position) {
            LogHelper.of("MixedRowModel-left-data").join(data).print();
            getView(R.id.icon).setBackgroundColor(data.getViewColor());
            ViewHelper.create(getView(R.id.icon2)).setBackgroundColor(data.getViewColor2());
        }
    }

    public static class RightMixedRowAdapter extends AbstractTypeAdapter<MixedRowBean, RightMixedRowHolder> {

        @NonNull
        @Override
        public RightMixedRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new RightMixedRowHolder(parent, R.layout.item_mixed_row_single);
        }
    }

    public static class RightMixedRowHolder extends AbstractEasyViewHolder<MixedRowBean> {

        public RightMixedRowHolder(@NonNull ViewGroup parent, int layoutId) {
            super(parent, layoutId);
            getView(R.id.icon).setClipToOutline(true);
            getView(R.id.icon).setOutlineProvider(new ViewOutlineProvider() {
                @Override
                public void getOutline(View view, Outline outline) {
                    outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(),
                            view.getResources().getDimension(R.dimen.dp_6));
                }
            });
        }

        @Override
        protected void bindLayout(MixedRowBean data, int position) {
            LogHelper.of("MixedRowModel-right-data").join(data).print();
            getView(R.id.icon).setBackgroundColor(data.getViewColor());
        }
    }
}
